package com.leetcode.LC;

public class LC917 {
    public String reverseOnlyLetters(String S) {
        //双指针分别放置头尾
        if (S.length()==1){
            return S;
        }
        char[] chars = S.toCharArray();
        int a = 0, b = S.length() - 1;
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char tmp;
        while (a < b) {
            if (str.contains(chars[a] + "") && str.contains(chars[b] + "")) {
                tmp = chars[a];
                chars[a] = chars[b];
                chars[b] = tmp;
                a++;
                b--;
            } else if (str.contains(chars[a] + "") && !str.contains(chars[b] + "")) {
                b--;
            } else if (!str.contains(chars[a] + "") && str.contains(chars[b] + "")) {
                a++;
            }else if (!str.contains(chars[a] + "") && !str.contains(chars[b] + "")) {
                a++;
                b--;
            }
        }

        return new String(chars);

    }

    public static void main(String[] args) {
        System.out.println(new LC917().reverseOnlyLetters("ab_c-abc"));
    }
}
